import React, { Component } from 'react'
import store2 from '../../store2/index'
// console.log('store2', store2)
// 引入actions
import { addProAction, addProActionAsync, getSchoollistAsync } from '../../store2/actions/shopAction'
export default class Father extends Component {
    constructor() {
        super()
        this.state = {
            ...store2.getState()
        }
        // 重新订阅
        // store2.subscribe(() => {
        //     this.setState({
        //         ...store2.getState()
        //     })
        // })
    }
    render() {
        const { shop, user } = this.state
        console.log(this.state);
        return (
            <div>
                {/* shop模块 */}
                <p>pronum:{shop.pronum}</p>
                <p>
                    <button onClick={() => this.addPronumFn(10)}>同步的+10</button>
                    <button onClick={() => this.addPronumAsyncFn(5)}>异步的+5</button>
                </p>
                {/* 渲染学校列表 */}
                <ul>
                    {
                        shop.schoollist.map(item => <li key={item.id}>{item.school_name}</li>)
                    }
                </ul>
                {/* user模块 */}
                <p>usernum:{user.usernum}</p>
            </div>
        )
    }
    // 同步方法
    addPronumFn(num) {
        // setTimeout(() => {
        store2.dispatch(addProAction(num))
        // }, 2000)
    }
    //页面中调用异步的action
    addPronumAsyncFn(payload) {
        store2.dispatch(addProActionAsync(payload))
    }

    componentDidMount() {
        // 页面初始化获取学校列表
        store2.dispatch(getSchoollistAsync())
    }

}
